setwd("~/Documents/replication_packages/thesis/")
db <- 'm5s_blog_mar2015.sqlite'
db_anonym <- 'm5s_blog_mar2015_anonymised.sqlite'

sqliteGetTable <- function(database, table) {
  require(DBI)
  require(RSQLite)
  con <- dbConnect(RSQLite::SQLite(), dbname = database)
  query <- dbSendQuery(con, paste("SELECT * FROM ", table, ";", sep="")) 
  result <- fetch(query, n = -1)
  dbClearResult(query)
  dbDisconnect(con)
  return(result)
}

sqliteListTables <- function(database, table) {
  require(DBI)
  require(RSQLite)
  con <- dbConnect(RSQLite::SQLite(), dbname = database)
  return(dbListTables(con))
}

sqliteWriteTable <- function(database, table_name, df) {
  require(DBI)
  require(RSQLite)
  con <- dbConnect(RSQLite::SQLite(), dbname = database)
  dbWriteTable(con, table_name, df)
}

tables <- sqliteListTables(db)

comment_tbl <- sqliteGetTable(db, 'comment')
post_tbl <- sqliteGetTable(db, 'post')
index_tbl <- sqliteGetTable(db, '[index]')

library(openssl)
comment_tbl$username <- md5(comment_tbl$username)

sqliteWriteTable(db_anonym, 'comment', comment_tbl)
sqliteWriteTable(db_anonym, 'post', post_tbl)
sqliteWriteTable(db_anonym, '[index]', index_tbl)
